package com.j2eefast.flowable.bpm.controller;

import com.j2eefast.common.core.business.annotaion.BussinessLog;
import com.j2eefast.common.core.enums.BusinessType;
import com.j2eefast.common.core.utils.*;
import com.j2eefast.flowable.bpm.entity.*;
import com.j2eefast.flowable.bpm.service.BpmTaskFromService;
import com.j2eefast.flowable.bpm.service.IFlowableTaskService;
import com.j2eefast.flowable.bpm.utils.BpmConstant;
import com.j2eefast.framework.annotation.RepeatSubmit;
import com.j2eefast.common.core.controller.BaseController;
import com.j2eefast.framework.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.ui.ModelMap;

import java.util.HashMap;
import java.util.Map;
import java.util.Date;
import org.springframework.web.bind.annotation.*;
import com.j2eefast.flowable.bpm.service.bpmCrmReimburseService;

/**
 * 报销页面控制器
 * @author yhli
 * @date 2020-06-28 18:30
 */
@Controller
@RequestMapping("/bpm/reimburse")
public class bpmCrmReimburseController extends BaseController{

    private String prefix = "modules/bpm/reimburse";
    @Autowired
    private bpmCrmReimburseService bpmCrmReimburseService;

    @Autowired
    private IFlowableTaskService flowableTaskService;
    /**
    * 必须注入实例关联表单服务
    */
    @Autowired
    private BpmTaskFromService bpmTaskFromService;


    @RequiresPermissions("bpm:reimburse:view")
    @GetMapping()
    public String reimburse(){
        return prefix + "/reimburse";
    }
        
    @RequestMapping("/list")
    @RequiresPermissions("bpm:reimburse:list")
    @ResponseBody
    public ResponseData list(@RequestParam Map<String, Object> params,bpmCrmReimburseEntity bpmCrmReimburseEntity) {
		PageUtil page = bpmCrmReimburseService.findPage(params,bpmCrmReimburseEntity);
		return success(page);
    }    
            
    @GetMapping("/add/{taskId}/{id}")
    public String add(@PathVariable(value="taskId", required=true)String taskId,@PathVariable(value="id", required=true)Long id,ModelMap mmp){
        BpmModelEntity data=  flowableTaskService.getVariables(taskId);

       BpmTaskEntity entity= flowableTaskService.getTasksByTaskId(taskId);
        //通过页面传入的表单ID查询表单关联信息
        BpmTaskFromEntity bpmTaskFrom = bpmTaskFromService.getTaskFromById(id);
        mmp.put("saleCode",data.getSaleCode());
        mmp.put("bpmTaskFrom", bpmTaskFrom);
        mmp.put("taskEntity", entity);
        mmp.put("user", UserUtils.getUserInfo());
        return prefix + "/add";
    }

    @GetMapping("/add")
    public String addCop(){
        return prefix + "/add";
    }
        /**
         * 定义关联表单申请表单URL对应此处
         */
//        @GetMapping("/add/{id}")
//        public String add(@PathVariable("id") Long id, ModelMap mmp){
//            BpmModelEntity data=  flowableTaskService.getVariables(id.toString());
//            //通过页面传入的表单ID查询表单关联信息
//            BpmTaskFromEntity bpmTaskFrom = bpmTaskFromService.getTaskFromById(id);
//            mmp.put("bpmTaskFrom", bpmTaskFrom);
//            mmp.put("saleCode",data.getSaleCode());
//            mmp.put("user", UserUtils.getUserInfo());
//            return prefix + "/add";
//        }

    /**
     * 新增
     */
    @RepeatSubmit
    @RequiresPermissions("bpm:reimburse:add")
    @BussinessLog(title = "报销", businessType = BusinessType.INSERT)
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData addbpmCrmReimburse(@Validated bpmCrmReimburseEntity bpmCrmReimburse,@Validated CompleteTaskEntity params){
        //校验参数
        //ValidatorUtil.validateEntity(bpmCrmReimburse);
        ResponseData result= bpmCrmReimburseService.savebpmCrmReimburse(bpmCrmReimburse)? success(): error("新增失败!");
        Map<String,Object> mp =new HashMap<>();
        mp.put(BpmConstant.IS_CANCEL,0);
        flowableTaskService.complete(params);
        return result;
    }    
    
    /**
     * 修改
     */
    @RequiresPermissions("bpm:reimburse:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap){
        bpmCrmReimburseEntity bpmCrmReimburse = bpmCrmReimburseService.getbpmCrmReimburseById(id);
        mmap.put("bpmCrmReimburse", bpmCrmReimburse);
        return prefix + "/edit";
    }
    /**
     * 审批1
     */
    @RequiresPermissions("bpm:reimburse:view1")
    @GetMapping("/view1/{id}")
    public String view1(@PathVariable("id") String id, ModelMap mmap){
        bpmCrmReimburseEntity bpmCrmReimburse = bpmCrmReimburseService.getbpmCrmReimburseById(id);
        mmap.put("bpmCrmReimburse", bpmCrmReimburse);
        return prefix + "/view1";
    }
    /**
     * 审批2
     */
    @RequiresPermissions("bpm:reimburse:view2")
    @GetMapping("/view2/{id}")
    public String view2(@PathVariable("id") String id, ModelMap mmap){
        bpmCrmReimburseEntity bpmCrmReimburse = bpmCrmReimburseService.getbpmCrmReimburseById(id);
        mmap.put("bpmCrmReimburse", bpmCrmReimburse);
        return prefix + "/view2";
    }
    /**
     * 审批3
     */
    @RequiresPermissions("bpm:reimburse:view3")
    @GetMapping("/view3/{id}")
    public String view3(@PathVariable("id") String id, ModelMap mmap){
        bpmCrmReimburseEntity bpmCrmReimburse = bpmCrmReimburseService.getbpmCrmReimburseById(id);
        mmap.put("bpmCrmReimburse", bpmCrmReimburse);
        return prefix + "/view3";
    }
    /**
     * 审批4
     */
    @RequiresPermissions("bpm:reimburse:view4")
    @GetMapping("/view4/{id}")
    public String view4(@PathVariable("id") String id, ModelMap mmap){
        bpmCrmReimburseEntity bpmCrmReimburse = bpmCrmReimburseService.getbpmCrmReimburseById(id);
        mmap.put("bpmCrmReimburse", bpmCrmReimburse);
        return prefix + "/view4";
    }

    /**
     * 修改保存报销
     */
    @RepeatSubmit
    @RequiresPermissions("bpm:reimburse:edit")
    @BussinessLog(title = "报销", businessType = BusinessType.UPDATE)
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData editbpmCrmReimburse(bpmCrmReimburseEntity bpmCrmReimburse){
		//ValidatorUtil.validateEntity(bpmCrmReimburse);
        return bpmCrmReimburseService.updatebpmCrmReimburseById(bpmCrmReimburse)? success(): error("修改失败!");
    }    

    /**
     * 删除
     */
    @RepeatSubmit
    @BussinessLog(title = "报销", businessType = BusinessType.DELETE)
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    @RequiresPermissions("bpm:reimburse:del")
    @ResponseBody
    public ResponseData del(String[] ids) {
      return bpmCrmReimburseService.deletebpmCrmReimburseByIds(ids)? success(): error("删除失败!");
    }    
}
